const connections = require("../app/database");
class GoodsService {
  // 删除商品
  async delgoodService(info) {
    console.log("del");
    // console.log(info.id);
    const statement = `DELETE FROM sp_goods_copy1 WHERE goods_id = ?;`;
    const res = connections.execute(statement, [info.id]);
    return res;
  }
  // 添加商品
  async updategoodService(info) {
    // console.log(info);
    const statement = `UPDATE sp_goods_copy1 
    SET goods_name=?,goods_price=?,goods_number=?,goods_weight=?,goods_introduce=? 
    where goods_id =?;`;
    const res = connections.execute(statement, [
      info.goods_name,
      +info.goods_price,
      +info.goods_number,
      +info.goods_weight,
      info.goods_introduce,
      +info.goods_id,
    ]);
    return res;
  }

  // 查询商品列表
  async getGoodlistService(info) {
    // console.log(info);
    // const statement = "";
    let statement;
    if (info.level == 0) {
      statement = `SELECT * 
      from sp_goods_copy1
      where cat_one_id = ? `;
    } else if (info.level == 1) {
      statement = `SELECT * 
      from sp_goods_copy1
      where cat_two_id = ? `;
    }
    // console.log(statement);
    const res = await connections.execute(statement, [info.cat_id]);
    return res[0];
  }

  // 获取对应级别1/0权限
  async getListService(num) {
    const statement = `SELECT * from sp_category_copy1
        where cat_level =?;`;
    const res = await connections.execute(statement, [num]);
    return res[0];
  }
  async addListService(info) {
    console.log(info);
    const statement = `insert into sp_category_copy1(cat_name,cat_pid,cat_level) values (?,?,?);`;
    const res = await connections.execute(statement, [
      info.cat_name,
      +info.cat_pid,
      +info.cat_level,
    ]);
    // const res = await connections.execute(statement, ["oppo", 0, 1]);
    return res;
  }
  async updateListService(info) {
    const statement = ` UPDATE sp_category_copy1 SET cat_name =? WHERE cat_id = ?;`;
    const res = await connections.execute(statement, [
      info.cat_name,
      info.cat_id,
    ]);
    return res;
  }
  async deleteListService(id) {
    console.log(id);
    const statement = `DELETE FROM sp_category_copy1 WHERE cat_id = ?;
    `;
    const res = connections.execute(statement, [+id]);
    return res;
  }
  async addGoodService(info) {
    // console.log(info);
    const statement = `INSERT INTO graduation.sp_goods_copy1
    ( goods_name, goods_price, goods_number, goods_weight, cat_id, goods_introduce, cat_one_id, cat_two_id) VALUES 
    ( ?, ?, ?, ?, ?, ?, ?, ?);`;
    console.log(typeof info.goods_weight);
    const res = await connections.execute(statement, [
      info.goods_name,
      +info.goods_price,
      +info.goods_number,
      +info.goods_weight,
      +info.goods_cat[1],
      info.goods_introduce,
      +info.goods_cat[0],
      +info.goods_cat[1],
    ]);
    return res;
  }

  async updateAvatarUrlById(url, goods_id) {
    const statement = `UPDATE sp_goods_copy1 SET avatar_url = ? WHERE goods_id = ?;`;
    const [result] = await connections.execute(statement, [url, +goods_id]);
    return result;
  }
}
module.exports = new GoodsService();
